<?php
class XuanWuSms {
	private $account="AA00388" ;
	private $passwd="AA0038806" ;
	//private $url = "http://211.147.239.62/Service/WebService.asmx?wsdl";
	//华信科技地址
	private $url = "http://dx.ipyy.net/sms.aspx ";
	private $port = "";
	private $type = "1";
	private $subid = "";
	private static $single = null;
	function __construct($account,$passwd,$type = 1) {
		$this->account = "AA00388";
		$this->passwd ="AA0038806";
// 		if (! class_exists ( "SoapClient" )) {
// 			die ( "请开启Soap扩展" );
// 			exit ();
// 		}
// 		if (self::$single == null) {
// 			self::$single = new SoapClient ( $this->url );
// 		}
	}
	function _postSingle($mobile, $content) {
		$params = array (
				'account' => $this->account,
				'password' => $this->passwd,
				'mobile' => $mobile,
				'content' => $content,
				'action'=>'send'
		);
		
		$result=$this->http_post($this->url,$params);
// 		echo $this->url; 
// print_r($params);
// print_r($result);
// 		die();
		$res = @simplexml_load_string($result,NULL,LIBXML_NOCDATA);
		$res = json_decode(json_encode($res),true);
		return $res;
      //  return $this->xml_to_array($result);
		//$result = self::$single->PostSingle ( $params );
		//return self::_getCodeStatus($result->PostSingleResult);
	}
	
	function xml_to_array( $xml )
	{
		$reg = "/<(\\w+)[^>]*?>([\\x00-\\xFF]*?)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches))
		{
			$count = count($matches[0]);
			$arr = array();
			for($i = 0; $i < $count; $i++)
			{
			$key= $matches[1][$i];
			$val = $this->xml_to_array( $matches[2][$i] );  // 递归
			if(array_key_exists($key, $arr))
			{
			if(is_array($arr[$key]))
			{
			if(!array_key_exists(0,$arr[$key]))
			{
			$arr[$key] = array($arr[$key]);
			}
			}else{
				$arr[$key] = array($arr[$key]);
			}
			$arr[$key][] = $val;
				}else{
					$arr[$key] = $val;
				}
				}
				return $arr;
				}else{
				return $xml;
			}
	}
	
	function http_post($url, $post_array = array(), $ctime = 3, $timeout = 4)
	{
		if (!is_array($post_array))
			return FALSE;
	
		$post_data = '';
		foreach ($post_array as $key => $var)
		{
			$post_data .= $key . '=' . urlencode($var) . '&';
		}
		$post_data = substr($post_data, 0, -1);
	
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $ctime);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_POST, TRUE);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}
	
	function _getAccountInfo(){
		$params = array (
				'account' => $this->account,
				'password' => $this->passwd
		);
		$r = self::$single->GetAccountInfo ($params);
		$data =  array();
		$data =  $this->object2array($r);
		return $data['GetAccountInfoResult'];
	}
	function object2array($object) {
		$array = array();
		if (is_object($object)) {
			foreach ($object as $key => $value) {
				if(is_object($value)){
					$array[$key] = $this->object2array($value);
				}else{
					$array[$key] = $value;
				}
			}
		}else {
			$array = $object;
		}
		return $array;
	}
	static function _getCodeStatus($status) {
		$arrCode = self::reponseCode ();
		return $arrCode [$status];
	}
	private static function reponseCode() {
		return array (
				"0" => "操作成功",
				"-1" => "账号无效",
				"-2" => "参数：无效",
				"-3" => "连接不上服务器",
				"-5" => "无效的短信数据，号码格式不对",
				"-6" => "用户名密码错误",
				"-7" => "旧密码不正确",
				"-9" => "资金账户不存在",
				"-11" => "包号码数量超过最大限制",
				"-12" => "余额不足",
				"-13" => "账号没有发送权限",
				"-99" => "系统内部错误",
				"-100" => "其它错误"
		);
	}
}
?>